package autoorder.zsxmlv.service;

import autoorder.zsxmlv.mapper.WechatUserMapper;
import autoorder.zsxmlv.po.WechatUser;
import common.zsxmlv.utils.ResultUtil;
import common.zsxmlv.vo.Result;
import common.zsxmlv.vo.Table;
import mp.weixin.vo.MpPush;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: ZSXM
 * @Description:
 * @Company: QIDI
 * @Date: Create by 2018/9/3 19:12
 * @Version: 1.0
 * @Modify by:
 */
@Service
@Transactional
public class WechatUserService {

    @Resource
    private WechatUserMapper userMapper;

    /**
     * 新增或更新粉丝
     * @return
     */
    public Result insertOrUpdate() throws Exception {
        Result result = new Result();
//        try {
            String accessToken = MpPush.getAccessToken();
            String userList = MpPush.getUserList(accessToken);
            JSONObject jsonObject = JSONObject.fromObject(userList);
            String data = jsonObject.get("data").toString();
            JSONObject jsonObject1 = JSONObject.fromObject(data);
            String openid = jsonObject1.get("openid").toString();
            JSONArray jsonArray = JSONArray.fromObject(openid);
            for (int i = 0; i < jsonArray.size(); i++) {
                openid = jsonArray.get(i).toString();
                String userInfo = MpPush.getUserInfo(accessToken, openid);
                JSONObject object = JSONObject.fromObject(userInfo);
                String headimgurl = object.get("headimgurl").toString();
                String nickname = object.get("nickname").toString();
                System.out.println(nickname + " -- " + openid);
                WechatUser wechatUser = userMapper.selectByOpenid(openid);
                if (wechatUser == null) {
                    wechatUser = new WechatUser(openid, headimgurl, nickname);
                    userMapper.insert(wechatUser);
                } else {
                    wechatUser.setHeadUrl(headimgurl);
                    wechatUser.setUserNick(nickname);
                    userMapper.updateByPrimaryKeySelective(wechatUser);
                }
            }
            result.setMsg("更新成功");
            result.setSuccess(true);
//        } catch (Exception e) {
//            System.err.println(e.getMessage());
//            e.printStackTrace();
//            result.setMsg("获取用户数据失败");
//            result.setSuccess(false);
//        }
        return result;
    }

    /**
     * 设置或者取消设置通知用户
     * @param openid
     * @param flag
     * @return
     */
    public Result setOrUnsetUser(String openid, Integer flag) {
        Result result = new Result();
        WechatUser wechatUser = userMapper.selectByOpenid(openid);
        if (flag == 1) {
            wechatUser.setIsSet(1);
            userMapper.updateByPrimaryKey(wechatUser);
            result.setSuccess(true);
            result.setMsg("操作成功！");
        } else if (flag == 0) {
            wechatUser.setIsSet(0);
            userMapper.updateByPrimaryKey(wechatUser);
            result.setSuccess(true);
            result.setMsg("操作成功！");
        } else {
            result.setSuccess(false);
            result.setMsg("请求失败,请稍后重试!");
        }
        return result;
    }

    /**
     * 获取所有用户
     * @return
     */
    public Table selectAllUser() {
        List<WechatUser> list = userMapper.selectAllUser();
        return ResultUtil.getResultTableByPageLimit(list, list.size());
    }


}
